<template>
  <div class="app-container">

    <!-- 数据表格 -->
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      fit
      highlight-current-row
      size="small"
      style="width: 100%;"
    >
      <el-table-column align="center" fixed label="序号" type="index"/>
      <el-table-column align="center" label="油站名称" prop="stationName" show-overflow-tooltip/>
      <el-table-column align="center" label="无中控油枪数量" prop="noControlNumbers"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="200">
        <template slot-scope="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              v-hasPermi="['base:no:fuel:data:update']"
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              @click="handleOpenDialog('update', scope.row)"/>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增-编辑对话框 -->
    <el-dialog
      v-loading="dialogLoading"
      :close-on-click-modal="false"
      :title="dialogTitleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      element-loading-text="保存中"
    >
      <el-form ref="dataForm" :model="dataForm" size="small">
        <el-form-item label="所选油站：" prop="stationName"> {{ dataForm.stationName }}</el-form-item>
        <el-form-item label="选择油枪">
          <el-checkbox v-model="checkAll" @change="handleCheckAllChange">全选
          </el-checkbox>
          <div style="margin: 15px 0;"/>
          <el-checkbox-group v-model="gunNoCheckedList" @change="handleCheckedGunNoChange">
            <el-checkbox v-for="item in gunList" :key="item.gunNo" :label="item.gunNo + ''">{{ item.gunNo }} /
              {{ item.fuelName }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="handleUpdate">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {fetchList, update} from '@/api/base/NofuelControl'
import {selectList} from '@/api/base/gun'

export default {


  name: 'Station',
  data() {
    return {
      dataList: [],
      dataListLoading: true,
      dialogFormVisible: false,
      dialogStatus: '',
      dialogLoading: false,
      dialogTitleMap: {
        add: '新增',
        update: '编辑'
      },
      dataForm: {
        id: '',
        gunNoIds: ''
      },
      gunNoCheckedList: [],
      checkAll: false,
      checkAllGunList: [],
      gunList: [],
      isIndeterminate: true
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 手动重置表单
    resetDataForm() {
      if (this.$refs['dataForm']) {
        // 由于编辑数据的时候会导致表单对象的初始化值发生变更，所以在重置表单前先手动重置表单对象的值
        this.dataForm = {
          id: '',
          gunNoCheckedList: []
        }
        this.$refs['dataForm'].resetFields()
      }
    },
    handleCheckAllChange(val) {
      this.gunNoCheckedList = val ? this.checkAllGunList : []
      this.isIndeterminate = false
    },
    handleCheckedGunNoChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.gunList.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.gunList.length
    },
    resetQueryForm() {
      this.$refs['queryForm'].resetFields()
      this.queryParams.page = 1
      this.getDataList()
    },
    getDataList() {
      this.dataListLoading = true
      fetchList()
        .then(res => {
          console.log(res)
          this.dataList = res.data
          this.dataListLoading = false
          this.$message.success(res.msg || '获取数据成功')
        })
        .catch(err => {
          console.log(err)
          this.dataListLoading = false
        })
    },
    handleOpenDialog(type, rowData) {
      // 表单状态为新增/编辑、打开新增/编辑窗口
      this.resetDataForm()
      this.dialogStatus = type
      this.dialogFormVisible = true
      if (type === 'update') {
        this.dataForm = JSON.parse(JSON.stringify(rowData))
        console.log(this.dataForm.gunNoIds)
        this.gunNoCheckedList = this.dataForm.gunNoIds.split(',')
        console.log(this.dataForm.gunNoIds.split(','))
        this.checkAll = false
        this.checkAllGunList = []
        selectList(this.dataForm.id)
          .then(res => {
            console.log(res)
            this.gunList = res.data
            for (const gunListKey in this.gunList) {
              this.checkAllGunList.push(this.gunList[gunListKey].gunNo)
            }
          })
      }
    },
    handleUpdate() {
      this.gunNoCheckedList = this.gunNoCheckedList.filter(item => {
        return !!item
      })
      this.dataForm.gunNoIds = this.gunNoCheckedList.join(',')
      this.dialogLoading = true
      update(this.dataForm)
        .then(res => {
          console.log(res)
          this.$message.success(res.msg || '修改成功')
          this.dialogLoading = false
          this.dialogFormVisible = false
          this.resetDataForm()
          this.getDataList()
        })
        .catch(err => {
          console.log(err)
          this.dialogLoading = false
        })
    }
  }
}
</script>
<style scoped>
</style>
